package com.bsc.rtc.lib.libraries.RxJavaRetrofitDemo;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.bsc.rtc.lib.libraries.R;
import com.bsc.rtc.lib.libraries.RxJavaRetrofitDemo.ServiceInterfaces.RetrofitRequest;
import com.bsc.rtc.lib.libraries.RxJavaRetrofitDemo.entity.Translation;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class RetrofitActivity extends AppCompatActivity {
    private String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit);
    }

    public void onRClick(View view) {
        switch (view.getId()) {
            case R.id.getCall:
                //getCall();
               // getCall2();
                getCall3();
                break;
        }
    }

    private void getCall() {
        Observable.interval(2, 2, TimeUnit.SECONDS).doOnNext(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                Log.i(TAG, "第 " + aLong + " 次轮询");
                RetrofitRequest.getInstance().getCall(new Observer<Translation>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.i(TAG, "onSubscribe");
                    }

                    @Override
                    public void onNext(Translation translation) {

                        translation.show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.i(TAG, "onComplete");
                    }
                });
            }
        }).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long aLong) {
                Log.i(TAG, "第=== " + aLong + " 次轮询");
            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, "第onError " + e.getMessage());
            }

            @Override
            public void onComplete() {
                Log.i(TAG, "onComplete== ");
            }
        });

    }

    private String flag = "";

    private void getCall2() {
        RetrofitRequest.getInstance()
                .getCall2()
                .doOnComplete(new Action() {
            @Override
            public void run() throws Exception {
                Log.i(TAG, "出发重订阅");
            }
        }).repeatWhen(new Function<Observable<Object>, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(Observable<Object> objectObservable) throws Exception {
                        return objectObservable.flatMap(new Function<Object, ObservableSource<?>>() {
                            @Override
                            public ObservableSource<?> apply(Object o) throws Exception {
                                if (o instanceof Translation) {
                                    Log.i(TAG, "o is Translation");
                                } else {
                                    Log.i(TAG, "o is not Translation");
                                }
                                Log.i(TAG, "修正订阅");
                                if (!flag.equals("嗨世界")) {
                                    return Observable.timer(2, TimeUnit.SECONDS);
                                } else {
                                    return Observable.empty();
                                }

                            }
                        });
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Translation>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.i(TAG, "返回订阅");
                    }

                    @Override
                    public void onNext(Translation translation) {
                        Log.i(TAG, "返回订阅");
                        translation.show();
                        flag = translation.getContent();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i(TAG, "返回订阅 onError" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.i(TAG, "返回订阅 onComplete");
                    }
                });
    }

    private void getCall3(){
        final Observable<Translation> observable3 = RetrofitRequest.getInstance().getCall3();
        RetrofitRequest.getInstance()
                .getCall2()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Translation>() {
            @Override
            public void accept(Translation translation) throws Exception {
                Log.i(TAG, "on next==" + translation.getContent());
                Log.d(TAG, "第1次网络请求成功");
            }
        }) .observeOn(Schedulers.io())
                .flatMap(new Function<Translation, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(Translation translation) throws Exception {
                        Log.i(TAG, "flatMap " + translation.getContent());
                        return observable3;
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {

                    @Override
                    public void accept(Object translation) throws Exception {
                        Log.d(TAG, "第2次网络请求成功");
                    }
                });
    }
}



